﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fiddler;

namespace ProgramCat.FiddlerExtension
{
    public class GenerateCode_YcWinHttpR : GenerateCodeBase
    {
        public override string GenerateCode(int compilingMode, Session session)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(".版本 2\r\n\r\n");
            stringBuilder.AppendLine(".子程序 功能_网页访问, 文本型, , 本子程序由编程猫内部插件生成,请配合 [鱼刺类_Httpv5.0] 使用。");
            stringBuilder.AppendLine(".局部变量 局_网址, 文本型");
            stringBuilder.AppendLine(".局部变量  http, WinHttpR");
            stringBuilder.AppendLine(".局部变量  局_提交数据, 文本型");
            stringBuilder.AppendLine(".局部变量  局_提交协议头, 文本型");
            stringBuilder.AppendLine(".局部变量  局_提交cookie, 文本型");
            stringBuilder.AppendLine(".局部变量  局_返回, 文本型");

            string fullUrl = session.fullUrl;
            stringBuilder.AppendLine(string.Format("局_网址=\"{0}\"", fullUrl));

            string body = System.Text.Encoding.UTF8.GetString(session.RequestBody);
            if (body != "")
            {
                body = body.Replace("\"", "\" + #引号 + \"");
                stringBuilder.AppendLine(string.Format("局_提交数据 = \"{0}\"", body));
            }

            stringBuilder.AppendLine("http.Auto(真)");
            stringBuilder.AppendLine(string.Format("http.Open (\"{0}\", 局_网址)", session.RequestMethod));

            GenerateCodeHelp.GenerateHeadAndCookies_Yc(session, ref stringBuilder, compilingMode);
            stringBuilder.AppendLine("http.SetCookie (局_提交cookie)");
            stringBuilder.AppendLine("http.Send (局_提交数据)");
            stringBuilder.AppendLine("http.GetResponseTextU2A()");
            stringBuilder.AppendLine("局_返回 ＝ 到文本 (http.GetResponseBody())");
            stringBuilder.AppendLine("返回(局_返回)");

            return stringBuilder.ToString();
        }
    }
}
